/*
 * Project Name demo
 * File Name SocketCache
 * Package Name com.huxiaosu.demo.socketio
 * Create Time 2023/1/29
 * Create by name：liujie
 */
package com.huxiaosu.demo.socketio.util;

import com.corundumstudio.socketio.SocketIOClient;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Description
 *
 * @ClassName: SocketCache
 * @author: liujie
 * @date: 2023/1/29 10:06
 */
@Slf4j
public class SocketCache {
    private static ConcurrentMap<Integer, SocketIOClient> connectMap;
    private SocketCache(){
        connectMap = new ConcurrentHashMap<>();
    }
    private static volatile SocketCache socketCache;
    public static SocketCache getInstance(){
        if(socketCache==null){
            synchronized (SocketCache.class){
                if(socketCache == null){
                    socketCache = new SocketCache();
                }
            }
        }
        return socketCache;
    }


    public void addClient(final Integer key,final SocketIOClient client){

        log.info("add client ;client id = {}",key);
        connectMap.put(key,client);
    }
    public SocketIOClient getClient(final Integer key){
        log.info("client all ;connectMap = {}",connectMap);
        return connectMap.getOrDefault(key,null);
    }

    public void removeClient(final Integer key){
        connectMap.remove(key);
    }

    public ConcurrentMap<Integer, SocketIOClient> getAll() {

        return connectMap;
    }
}